<template>
  <div>
    <el-row :gutter="20" style="margin: 10px 10px">
      <el-col :span="12">
        <el-card class="area">
          <div slot="header" class="clearfix">
            <span>介绍</span>
          </div>
          <div>
            <ul style="margin: 0px">
              <li>
                开发手册：下载&nbsp;<el-button
                  type="text"
                  @click="down('/doc/jwater开发手册-1.0.pdf')"
                  >jwater开发手册-1.0.pdf</el-button
                >
              </li>
              <li>
                运维手册：下载&nbsp;<el-button
                  type="text"
                  @click="down('/doc/jwater运维手册-1.0.pdf')"
                  >jwater运维手册-1.0.pdf</el-button
                >
              </li>
              <li>
                工作进程配置模版：查看&nbsp;<el-button
                  type="text"
                  @click="show('/template/jwater.worker.xml')"
                  >jwater.worker.xml</el-button
                >
              </li>
              <li>
                定时任务配置模版：查看&nbsp;<el-button
                  type="text"
                  @click="show('/template/jwater.job.xml')"
                  >jwater.job.xml</el-button
                >
              </li>
              <li>
                日志配置模版：查看&nbsp;<el-button
                  type="text"
                  @click="show('/template/logback.xml')"
                  >logback.xml</el-button
                >
              </li>
              <li>
                程序安装包示例：下载&nbsp;<el-button
                  type="text"
                  @click="down('/samples/install/simple-samples.zip')"
                  >simple-samples.zip</el-button
                ><el-button
                  type="text"
                  @click="down('/samples/install/spring-samples.zip')"
                  >spring-samples.zip</el-button
                >
              </li>
              <li>
                程序示例源码：下载&nbsp;<el-button
                  type="text"
                  @click="down('/samples/src/jwater-simple-samples.zip')"
                  >jwater-simple-samples.zip</el-button
                ><el-button
                  type="text"
                  @click="down('/samples/src/jwater-spring-samples.zip')"
                  >jwater-spring-samples.zip</el-button
                >
              </li>
              <li>
                <a @click="show('/license/LICENSE')" style="color: #46a6ff"
                  >License</a
                >
                &nbsp;
                <span
                  >jwater是一款java程序、微服务程序的轻量级运维部署平台，支持springboot、springcloud，能更好的解决微服务的运维部署问题。跟k8s、云原生等相关技术相比，jwater使用没有门槛，kubernates是devops标准解决方案，jwater目前只专注于java程序运维部署。</span
                >
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="area">
          <div slot="header" class="clearfix">
            <span>任务汇总</span>
            <el-button
              style="float: right; padding: 3px 0"
              icon="el-icon-refresh"
              @click="showPackageList"
            ></el-button>
          </div>
          <div>
            <el-table v-loading="loading" :data="packageInfos">
              <el-table-column label="域" prop="regionName" align="left" />
              <el-table-column
                label="安装包数量"
                prop="packageCount"
                align="center"
              />
              <el-table-column
                label="工作进程数量"
                prop="workerCount"
                align="center"
              />
              <el-table-column
                label="在线进程实例数量"
                prop="workerOnlineCount"
                align="center"
              />
              <el-table-column
                label="定时任务数量"
                prop="jobCount"
                align="center"
              />
              <el-table-column
                label="已启动任务数量"
                prop="jobRunCount"
                align="center"
              />
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin: 10px 10px">
      <el-col :span="12">
        <el-card class="area">
          <div slot="header" class="clearfix">
            <span>告警信息</span>
            <el-button
              style="float: right; padding: 3px 0"
              icon="el-icon-refresh"
              @click="showAlarmList"
            ></el-button>
            <router-link to="/monitor/alarm">
              <el-button style="float: right; padding: 3px 5px" type="text"
                >详情</el-button
              >
            </router-link>
          </div>
          <div>
            <el-table v-loading="loading" :data="alarmInfos">
              <el-table-column
                label="主机"
                prop="host"
                align="left"
                min-width="120"
              />
              <el-table-column
                label="告警消息"
                prop="msg"
                align="center"
                min-width="300"
              />
              <el-table-column
                label="告警时间"
                prop="msg"
                align="center"
                min-width="150"
              >
                <template slot-scope="scope">{{
                  ft(scope.row.alarmTime)
                }}</template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="area">
          <div slot="header" class="clearfix">
            <span>主机监控</span>
            <el-button
              style="float: right; padding: 3px 0"
              icon="el-icon-refresh"
              @click="showHostList"
            ></el-button>
            <router-link to="/monitor/host">
              <el-button style="float: right; padding: 3px 5px" type="text"
                >详情</el-button
              >
            </router-link>
          </div>
          <div>
            <el-table v-loading="loading" :data="hostInfos">
              <el-table-column
                label="主机"
                prop="host"
                align="left"
                min-width="120"
              />
              <el-table-column label="总内存" align="center">
                <template slot-scope="scope">{{
                  mem(scope.row, "total")
                }}</template>
              </el-table-column>
              <el-table-column label="剩余内存" align="center">
                <template slot-scope="scope">{{
                  mem(scope.row, "free")
                }}</template>
              </el-table-column>
              <el-table-column label="CPU使用率" align="center">
                <template slot-scope="scope">{{ cpu(scope.row) }}</template>
              </el-table-column>
              <el-table-column label="磁盘剩余" align="center">
                <template slot-scope="scope">{{
                  fileSystemSpace(scope.row, "freeSpace")
                }}</template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 绑定菜单对话框 -->
    <el-dialog title="内容" :visible.sync="open" width="1000px" append-to-body>
      <el-form label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-input type="textarea" v-model="content" rows="20"></el-input>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAllSystemInfo,
  getAllAlarmInfo,
  getPackageInfo,
  getDownFileUrl,
  getContent,
} from "@/api/console";
import { formatDate } from "@/utils/index";
export default {
  name: "index",
  data() {
    return {
      loading: false,
      hostInfos: [],
      alarmInfos: [],
      packageInfos: [],
      content: "",
      open: false,
    };
  },
  created() {
    this.showHostList();
    this.showAlarmList();
    this.showPackageList();
  },
  methods: {
    showHostList() {
      getAllSystemInfo()
        .then((res) => {
          this.hostInfos = res.data;
        })
        .catch((msg) => {});
    },
    showAlarmList() {
      getAllAlarmInfo().then((res) => {
        this.alarmInfos = res.data;
      });
    },
    showPackageList() {
      getPackageInfo().then((res) => {
        this.packageInfos = res.data;
      });
    },
    mem(row, type) {
      const data = row.systemMemory;
      return (parseInt(data[type] * 10) * 1.0) / 10 + "M";
    },
    cpu(row) {
      return (parseInt(row.cpuUsage * 1000) * 1.0) / 10 + "%";
    },
    fileSystemSpace(row, type) {
      if (
        row.path &&
        row.allFileSystemSpace &&
        row.allFileSystemSpace[row.path]
      ) {
        const data = row.allFileSystemSpace[row.path];
        return (parseInt(data[type] * 10) * 1.0) / 10 + "G";
      }
      return 0 + "G";
    },
    ft(timestamp) {
      return formatDate(timestamp);
    },
    down(path) {
      console.log(path);
      this.$confirm("是否确认下载?", "确定", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((res) => {
        let aLink = document.createElement("a");
        aLink.style.display = "none";
        aLink.href = getDownFileUrl(path);
        aLink.setAttribute("download", path); // 下载的文件
        document.body.appendChild(aLink);
        aLink.click();
        document.body.removeChild(aLink);
      });
    },
    show(path) {
      //path= + encodeURIComponent(path);
      console.log(path);
      getContent(path).then((res) => {
        this.content = res.data;
      });
      this.open = true;
    },
    cancel() {
      this.open = false;
    },
  },
};
</script>
<style>
.area {
  height: 400px;
  background: #fff;
  border-radius: 4px;
  border: 1px solid #e5e9f2;
}
</style>
